using System.Collections.Generic;
using System.IO;
using Sixeyed.CodeGeneration.Metadata.Bases;
using Sixeyed.CodeGeneration.Metadata.FileSystem.Interfaces;
using Sixeyed.CodeGeneration.Metadata.FileSystem.Items;
using Sixeyed.CodeGeneration.Metadata.Interfaces;
using reflection = System.Reflection;
using Sixeyed.CodeGeneration.Metadata.FileSystem.Providers.Bases;

namespace Sixeyed.CodeGeneration.Metadata.FileSystem.Providers
{
    /// <summary>
    /// Provider for returning <see cref="Assembly"/> metadata items
    /// </summary>
    /// <remarks>
    /// Provider shows all available DLLs in the path Uri for selection.
    /// Full file path is used as the key for the metadata item.
    /// </remarks>
    public class AssemblyProvider : FileSystemProviderBase
    {
        /// <summary>
        /// Gets the provider name
        /// </summary>
        public override string Name
        {
            get { return "Assembly file provider"; }
        }

        /// <summary>
        /// Gets the provider description
        /// </summary>
        public override string Description
        {
            get { return "To use a .NET assembly as a metadata source"; }
        }

        /// <summary>
        /// Gets the name of the returned metadata item type
        /// </summary>
        public override string ItemTypeName
        {
            get { return "Assembly file"; }
        }

        /// <summary>
        /// Returns a list of available assemblies
        /// </summary>
        /// <returns>List of assemblies</returns>
        public override List<string> GetItemNames()
        {
            return this._source.GetFileList("*.dll");
        }

        /// <summary>
        /// Returns a populated metadata item
        /// </summary>
        /// <param name="itemKey">Path of .NET assembly file</param>
        /// <returns>Populated <see cref="Assembly"/></returns>
        public override IMetadataItem GetItem(string itemKey)
        {
            Assembly item = this.CreateItem<Assembly>(itemKey);
            item.TypedItem = reflection.Assembly.LoadFile(Path.Combine(this._source.Uri, itemKey));
            return item;
        }
    }
}
